using UnityEngine;
namespace CGF
{
    public partial class GameUI : UIComponentBase, IAwake<UILayer, bool>, IUpdate, IDestroy
    {
        public static GameUI instance;
        private GameObject[] prefabs = new GameObject[4];
        private GameObject[] tabs = new GameObject[4];

        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<GameUI>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<GameUI>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/GameUI", layer, isFloat);
            BindProperties(transform);
            instance = this;
            
            MainLaunch.Destroy();
            
            if (CGFConfig.Instance.ResServerType == EResServerType.FormalServer)
            {
                PushMsgClient.Instance.Start();
            }

            await GlobalData.LoadAllSellDevices();
            
            prefabs[0] = TabWallet;
            prefabs[1] = TabDevices;
            prefabs[2] = TabInvites;
            prefabs[3] = TabMe;

            SwitchTab(0);
            
            PanelGuide.Open();
        }

        public void Update()
        {
            if (Application.isEditor && Input.GetMouseButtonDown(1))
            {
                PopupSucSold.Open("23423423", "3000");
            }
        }

        public void SwitchTab(int index)
        {
            for (int i = 0; i < tabs.Length; i++)
            {
                if (i != index)
                {
                    if (tabs[i]) tabs[i].gameObject.SetActive(false);
                }
                else
                {
                    if (!tabs[i])
                    {
                        tabs[i] = GameObject.Instantiate(prefabs[i], Container, false);
                    }
                    tabs[i].gameObject.SetActive(true);
                }
            }
            Navigation.GetComponent<Navigation>().SetIndex(index);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            PushMsgClient.Instance.Stop();
        }

    }
}
